﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WqHtmlAnchorSpider
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private DbManager _dbManager = new DbManager();

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void BtStart_OnClick(object sender, RoutedEventArgs e)
        {
            BtStart.IsEnabled = false;
            SpiderAnchors();
            MessageBox.Show("查找完成");
            BtStart.IsEnabled = true;
            UpdatePercent(0);
        }

        private void SpiderAnchors()
        {
            string anchorType = String.Empty;
            if (rbSelfieSpider.IsChecked == true)
            {
                anchorType = AnchorType.Selfie.ToString();
            }

            if (string.IsNullOrWhiteSpace(anchorType))
            {
                return;
            }

            var urlsProvider = new UrlProviders204();
            var urls = urlsProvider.GetUrls(anchorType, int.Parse(tbBeginIndex.Text), int.Parse(tbEndIndex.Text));

            for (int i = 0; i < urls.Count; i++)
            {
                HtmlAnchorParser parser = new HtmlAnchorParser();
                List<AnchorObj> tempObjs = parser.GetAnchorObjs(anchorType, urls[i]);
                double percent = ((double)(i + 1)) / urls.Count * 100;
                UpdatePercent(percent);

                _dbManager.AddAnchors(tempObjs);
            }
        }

        private void UpdatePercent(double percent)
        {
            pbProgress.Value = percent;
            tbPercent.Text = percent + "%";
        }

        private void BtChoosePath_OnClick(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.ShowDialog();
            if (fbd.SelectedPath != string.Empty) this.tbSavePath.Text = fbd.SelectedPath;
        }

        private void BtGenarate_OnClick(object sender, RoutedEventArgs e)
        {
            String searchKey = tbSearch.Text;
            if (string.IsNullOrWhiteSpace(searchKey))
            {
                MessageBox.Show("请输入搜索关键词");
                return;
            }

            String saveFullPath;
            String savePath = tbSavePath.Text;
            if (string.IsNullOrWhiteSpace(tbSavePath.Text))
            {
                saveFullPath = searchKey + ".html";
                savePath = ".";
            }

            saveFullPath = savePath + "/" + searchKey + ".html";
            var list = _dbManager.FindAnchorObjsByName(searchKey);
            AnchorListHtmlWriter.WriteHtml(list, saveFullPath);
            Process.Start(savePath);
            MessageBox.Show("生成完成");
        }
    }
}